package com.hs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.entity.User;
import com.hs.mapper.UserMapper;
import com.hs.service.UserService;
import com.hs.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author libowen
 * @Date 2025年08月01日 15:39
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public int addUser(User user) {
        // 对密码进行加密
        user.setPassword(MD5Util.encode(user.getAccount()+user.getPassword()));
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(int id) {
        // 先查询是否有外键，如果有外键，则不可删除；
        return userMapper.deleteUser(id);
    }

    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public PageInfo<User> findUsers(User user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.findUsers(user);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public User login(String account, String password) {
        // 对登录的密码进行加密
        password = MD5Util.encode(account+password);
        return userMapper.findUserByAccountAndPassword(account,password);
    }
}
